<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Badge
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Badge_AdminManageController extends Core_Controller_Action_Admin {
	
	public function init() {
		
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			if (0 !== ($badge_id = ( int ) $this->_getParam ( 'badge_id' )) && null !== ($badge = Engine_Api::_ ()->getItem ( 'badge', $badge_id ))) {
				Engine_Api::_ ()->core ()->setSubject ( $badge );
			}
		}
		
		$this->_helper->requireSubject->setActionRequireTypes ( array ('delete' => 'badge', 'edit' => 'badge', 'photo' => 'badge' ) );
	}
	
	public function indexAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'badge_admin_main', array (), 'badge_admin_main_manage' );
		
		$this->view->formFilter = $formFilter = new Badge_Form_Admin_Manage_Filter ();
		
		// Process form
		$values = array ();
		if ($formFilter->isValid ( $this->_getAllParams () )) {
			$values = $formFilter->getValues ();
		}
		$values = Engine_Api::_ ()->badge ()->filterEmptyParams ( $values );
		
		$this->view->formValues = $values;
		
		$this->view->assign ( $values );
		
		$this->view->paginator = Engine_Api::_ ()->badge ()->getBadgesPaginator ( $values );
		$this->view->paginator->setItemCountPerPage ( 10 );
		$this->view->paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
		$this->view->params = $values;
	
	}
	
	public function createAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'badge_admin_main', array (), 'badge_admin_main_manage' );
		
		$this->view->form = $form = new Badge_Form_Admin_Manage_Create ();
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$table = Engine_Api::_ ()->getDbtable ( 'badges', 'badge' );
			
			$db = $table->getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$values = $form->getValues ();
				$values ['owner_id'] = $viewer->getIdentity ();
				
				$badge = $table->createRow ();
				$badge->setFromArray ( $values );
				$badge->save ();
				
				// Set photo
				if (! empty ( $values ['photo'] )) {
					$badge->setPhoto ( $form->photo );
				}
				
				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->_helper->redirector->gotoRoute ( array ('action' => 'index' ) );
		}
	
	}
	
	public function editAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'badge_admin_main', array (), 'badge_admin_main_manage' );
		
		$this->view->badge = $badge = Engine_Api::_ ()->core ()->getSubject ( 'badge' );
		
		$this->view->form = $form = new Badge_Form_Admin_Manage_Edit ();
		$form->populate ( $badge->toArray () );
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$table = Engine_Api::_ ()->getDbtable ( 'badges', 'badge' );
			
			$db = $table->getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$values = $form->getValues ();
				$badge->setFromArray ( $values );
				$badge->save ();
				
				// Set photo
				if (! empty ( $values ['photo'] )) {
					$badge->setPhoto ( $form->photo );
				}
				
				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$savedChangesNotice = Zend_Registry::get ( 'Zend_Translate' )->_ ( "Your changes were saved." );
			$form->addNotice ( $savedChangesNotice );
		
		//$this->_helper->redirector->gotoRoute(array('action' => 'index'));
		}
	
	}
	
	public function photoAction() {
		$this->view->badge = $badge = Engine_Api::_ ()->core ()->getSubject ( 'badge' );
	}
	
	public function deleteAction() {
		
		$this->view->badge = $badge = Engine_Api::_ ()->core ()->getSubject ( 'badge' );
		
		// Check post
		if ($this->getRequest ()->isPost ()) {
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$badge->delete ();
				$db->commit ();
				
				Engine_Api::_ ()->core ()->clearSubject ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => Zend_Registry::get ( 'Zend_Translate' )->_ ( "Badge deleted successfully." ) ) );
		}
	
	}

}